﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartMD.DataAccess;
using SmartMD.DataAccess.Oracle;
using System.Data;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.ComponentModel;
using SmartMD.Manager.Report;
using SmartMD.Manager.Specimens.Query;
using Techstar.Dialog;

namespace SmartMD.Manager.Specimens
{
    /// <summary>
    /// Interaction logic for QuerySpecPanel.xaml
    /// </summary>
    public partial class QuerySpecPanel : UserControl
    {
        public QuerySpecPanel()
        {
            InitializeComponent();
            this.ucSpecQueryEdit.Owner = this;
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            if (this.ucSpecQueryList.printListView == null || this.ucSpecQueryList.printListView.Count <= 0)
            {
                TaskDialogEx.Warning("提示", "请选择所需打印的标本！");
                return;
            }
            PrintService printService = new PrintService();
            List<SpecPrint> printList = new List<SpecPrint>();
            List<SpecQueryResult> result = this.ucSpecQueryList.printList;
            SpecQueryResult qResult;
            SpecPrint spec;
            for (int j = 0; j < this.ucSpecQueryList.printList.Count; j++)
            {
                 qResult = this.ucSpecQueryList.printList[j];
                 if (qResult.State == DataModel.RecordState.Printed)
                 {
                     spec = new SpecPrint();
                   //  spec.SpecId = qResult.Id;
                     spec.SpecId = qResult.SpecCode;
                     spec.SpecType = qResult.SpecType;
                     spec.SiteCode = qResult.SiteCode;
                     spec.PatientName = qResult.PatientName;
                     spec.StoreDate = qResult.FollowUpDate;
                     printList.Add(spec);
                 }         
            }
            if (printList.Count > 0)
            {
                printService.StartPrintPreview(printList);
            }

            List<SpecQueryResult> resultlist = this.ucSpecQueryList.printListView;
            if (resultlist.Count > 0)
            {
                PrintList(resultlist);
            }
        }

        public void OnWindowActivated()
        {
            this.ucSpecQueryEdit.InitAllComboxValue();
        }

        #region 打印列表(黄俊添加)
        /// <summary>
        /// //手動填加ListView方法1
        /// </summary>
        private void PrintList(List<Specimens.SpecQueryResult> result)
        {
            PrintListView printListView1 = new PrintListView();

            //创建标题
            printListView1.Columns.Add("标本编码");
            printListView1.Columns.Add("所属患者");
            printListView1.Columns.Add("所属课题");
            printListView1.Columns.Add("标本位置");
            printListView1.Columns.Add("随访人");
            printListView1.Columns.Add("随访周次");
            printListView1.Columns.Add("随访时间");
            printListView1.Columns.Add("标本类型");
            printListView1.Columns.Add("冻融次数");
            printListView1.Columns.Add("标本总量");

            printListView1.Columns[0].Width = 110;
            printListView1.Columns[1].Width = 110;
            printListView1.Columns[2].Width = 110;
            printListView1.Columns[3].Width = 110;
            printListView1.Columns[4].Width = 110;
            printListView1.Columns[5].Width = 110;
            printListView1.Columns[6].Width = 110;
            printListView1.Columns[7].Width = 110;
            printListView1.Columns[8].Width = 110;
            printListView1.Columns[9].Width = 110;

            System.Windows.Forms.ImageList imageList1 = new System.Windows.Forms.ImageList();
            
            //ListView顯示方式
            printListView1.View = System.Windows.Forms.View.Details;
            printListView1.LineSpace = 15;
            printListView1.BeginUpdate();

            int i = 1;
            foreach (Specimens.SpecQueryResult sqr in result)
            {
                printListView1.Items.Add(i.ToString(), sqr.SpecCode, 0);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.PatientName);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.Subject);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.SiteCode);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowerUper);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowUpTimes.ToString());
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowUpDate.ToString());
                printListView1.Items[i.ToString()].SubItems.Add(sqr.SpecType);
                printListView1.Items[i.ToString()].SubItems.Add("");
                printListView1.Items[i.ToString()].SubItems.Add("");
                i++;
            }

            printListView1.EndUpdate();
            printListView1.DoPrint();
        }
        #endregion

        private void btnSpecUsage_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("标本出库");
            if (this.ucSpecQueryList.printListView != null && this.ucSpecQueryList.printListView.Count > 0)
            {
                SpecUsageDialog dlgSpecUsage = new SpecUsageDialog(this);
                dlgSpecUsage.Show();
            }
            else
            {
                TaskDialogEx.Warning("提示", "请选择出库标本！");
            }
        }
    }
}
